package com.hctforgreen.greenservice.model;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Set;
import java.util.TreeSet;

/**
 * 6、用户登录
 * 
 * @author Tallen Zhai
 * 
 */
public class LoginResultEntity extends BaseEntity {
	private static final long serialVersionUID = 3095817584996924828L;

	public String statusCode = "";
	public String msg = "";

	/**
	 * 登录状态--- 0：正常； 1：用户不存在； 2：用户未激活 ；
	 * 3：用户已绑定其他手机；4：获取token失败；5：首次登录，需要激活码；7：验证码错误；

	 */
	public String loginStatus = "";
	/**
	 * 登录返回信息
	 */
	public String loginMsg = "";

	public String personId = "";
	public String personName = "";

	public Set<String> tags = null;// 极光推送分组标签

	public String functions = "";// 功能权限 1-9分别对应主页的9个菜单

	public String rank = "";// 排名情况
	public String rating = "";// 等级
	public String points = "";// 积分

	public String addPoints = "";

	public String assign;

	public String token = "";

	public final static String STATUS_CODE = "statusCode";
	public final static String MSG = "msg";
	public final static String ASSIGN = "assign";
	public final static String LOGIN_STATUS = "loginStatus";
	public final static String LOGIN_MSG = "loginMsg";

	public final static String PERSON_ID = "personId";
	public final static String PERSON_NAME = "personName";
	public final static String TAGS = "tags";

	public final static String FUNCTIONS = "functions";

	public final static String FUNCTION_TAG = "!";// 功能权限中间分隔符号

	public final static String FUNCTION_STARTPWD = "!1!";// 开机密码权限
	public final static String FUNCTION_CAPTURE = "!2!";// 条码扫描权限
	public final static String FUNCTION_DEBUG = "!3!";// 调试维修权限

	public final static String FUNCTION_MESSAGEFIRST = "!4!";// 通知权限
	public final static String FUNCTION_TECHNICALDATA = "!5!";// 技术资料权限
	public final static String FUNCTION_FEEDBACK = "!6!";// 信息反馈权限

	public final static String FUNCTION_DOWNLOADMANAGER = "!7!";// 下载管理权限
	public final static String FUNCTION_SETTING = "!8!";// 软件设置权限
	public final static String FUNCTION_VIDEO = "!9!";// 视频播放权限
	// =========页面中才用的到=========
	public String phone = "";

	public static LoginResultEntity parse(JSONObject jsonObject)
			throws JSONException {
		LoginResultEntity bean = new LoginResultEntity();
		if (jsonObject.has(STATUS_CODE)) {
			bean.statusCode = jsonObject.getString(STATUS_CODE);
		}
		if (jsonObject.has(MSG)) {
			bean.msg = jsonObject.getString(MSG);
		}
		if (jsonObject.has(LOGIN_STATUS)) {
			bean.loginStatus = jsonObject.getString(LOGIN_STATUS);
		}
		if (jsonObject.has(LOGIN_MSG)) {
			bean.loginMsg = jsonObject.getString(LOGIN_MSG);
		}
		if (jsonObject.has(ASSIGN)) {
			bean.assign = jsonObject.getString(ASSIGN);
		}

		if (jsonObject.has(PERSON_ID)) {
			bean.personId = jsonObject.getString(PERSON_ID);
		}
		if (jsonObject.has(PERSON_NAME)) {
			bean.personName = jsonObject.getString(PERSON_NAME);
		}
		if (jsonObject.has("rating")) {
			bean.rating = jsonObject.getString("rating");
		}
		if (jsonObject.has("rank")) {
			bean.rank = jsonObject.getString("rank");
		}
		if (jsonObject.has("points")) {
			bean.points = jsonObject.getString("points");
		}
		if (jsonObject.has("addPoints")) {
			bean.addPoints = jsonObject.getString("addPoints");
		}

		if (jsonObject.has("token")) {
			bean.token = jsonObject.getString("token");
		}
		if (jsonObject.has(TAGS)) {
			JSONArray jTags = jsonObject.getJSONArray(TAGS);
			if (jTags != null && jTags.length() > 0) {
				bean.tags = new TreeSet<String>();
				for (int i = 0; i < jTags.length(); i++) {
					bean.tags.add(jTags.getString(i));
				}
			}
		}

		if (jsonObject.has(FUNCTIONS)) {
			JSONArray jFunc = jsonObject.getJSONArray(FUNCTIONS);
			if (jFunc != null && jFunc.length() > 0) {
				for (int i = 0; i < jFunc.length(); i++) {
					bean.functions += FUNCTION_TAG + jFunc.getString(i)
							+ FUNCTION_TAG;

				}
			}
		}

		return bean;
	}
}
